{% extends "layout/base.html" %}
{% load static %}

{% block css %}
    <link rel="stylesheet" href="{% static 'plugin/editor-md/css/editormd.min.css' %}">
    <style>
        .panel-default {
            margin-top: 10px;
            border-color: #ddd;
        }

        .panel-heading {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            background-color: #f5f5f5;
            border-color: #ddd;
            font-size: 17px;
            padding: 5px;
        }

        .panel-body {
            padding: 0;
            border: 1px solid #dddddd;
        }

        .title-list {
            border-right: 1px solid #dddddd;
            min-height: 500px;
            padding-left: 10px;
        }

        .title-list ul {
            padding-left: 15px;
        }

        .title-list ul a {
            display: block;
            padding: 5px 0;
        }

        .content {
            border-left: 1px solid #dddddd;
            min-height: 600px;
            margin-left: -1px;
        }

        a {
            color: #337ab7;
            text-decoration: none;
        }

        .fluid {
            width: 100%;
            padding-right: var(--bs-gutter-x, .75rem);
            margin-right: auto;
            margin-left: auto;

        }

        .markdown-body img {
            max-width: 50%;
        }

    </style>

{% endblock %}


{% block content %}

    <div class="fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                <div>
                    <i class="fa fa-book" aria-hidden="true"></i> wiki文档
                </div>
                <div class="function">
                    <a type="button" class="btn btn-success btn-sm"
                       href="{% url 'wiki_add' %}">
                        <i class="fa fa-plus-circle" aria-hidden="true"></i> 新建
                    </a>
                    {% if wiki_object %}
                        <a type="button" class="btn btn-primary btn-sm"
                           href="{% url 'wiki_edit' wiki_id=wiki_object.id %}">
                            <i class="fa fa-edit" aria-hidden="true"></i> 编辑
                        </a>
                        <a type="button" class="btn btn-danger btn-sm"
                           href="javascript:if(confirm('确认删除吗?'))window.location='{% url 'delete_wiki' wiki_id=wiki_object.pk %}'"><i
                                class="fa fa-trash" aria-hidden="true"></i>&nbsp;删除</a>
                    {% endif %}

                </div>
            </div>
            <div class="panel-body" style="display: flex;">
                <div class="col-sm-3 title-list">
                    <ul class="item">
                        {% for i in request.tracer.project %}
                            <li id="id_{{ i.pk }}">
                                <a href="javascript:void(0)" style="cursor: default">{{ i.it_name }}</a>
                                <ul></ul>
                            </li>
                        {% endfor %}
                    </ul>
                </div>
                <div class="col-sm-9 content">
                    {% if wiki_object %}
                        <div id="previewMarkdown">
                            <textarea> {{ wiki_object.content }}</textarea>
                        </div>
                    {% else %}
                        <div style="text-align: center;margin-top: 50px;">
                            <h4>wiki文档库 </h4>
                            <a href="{% url 'wiki_add' %}">
                                <i class="fa fa-plus-circle" aria-hidden="true"></i> 新建文章
                            </a>
                        </div>
                    {% endif %}
                </div>
                <div></div>
            </div>
        </div>
    </div>

{% endblock %}

{% block js %}
    <script src="{% static 'plugin/editor-md/editormd.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/marked.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/prettify.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/raphael.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/underscore.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/sequence-diagram.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/flowchart.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/jquery.flowchart.min.js' %}"></script>
    <script>
        var WIKI_DETAIL_URL = "{% url 'wiki'%}";
        $(function () {
            initCatalog();
            initPreviewMarkdown();
            bindClickDelete();
        });

        function initPreviewMarkdown() {
            editormd.markdownToHTML("previewMarkdown", {
                // 内容过滤，防止xss攻击等
                htmlDecode: "style,script,iframe",
            });
        }

        /*左侧项目初始化*/
        function initCatalog() {
            $.ajax({
                url: "{% url 'wiki_catalog' %}",
                type: "GET",
                dataType: "JSON",
                success: function (res) {
                    if (res) {
                        $.each(res.wiki_data, function (index, item) {
                            var href = WIKI_DETAIL_URL + "?wiki_id=" + item.id;
                            var li = $("<li>").attr('id', "id_" + item.id).append($('<a>').text(item.title).attr('href', href)).append($('<ul>'));
                            $("#id_" + item.project_id).children("ul").append(li)
                        })
                    } else {
                        alert("初始化目录失败")
                    }
                }
            })
        }

        /*
        删除二次确认
         */
        function bindClickDelete() {
            $("#btnSubmit").click(function () {
                $.ajax({
                    url: "{% url 'delete_api' %}",
                    data: $("#delete_api_id").serialize(),
                    type: "POST",
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            window.location.href = location.href;
                        } else {
                            alert("删除失败")
                        }
                    }
                })
            })
        }

    </script>



{% endblock %}